package ms.maplestory.boot.world.field.objects;


import ms.maplestory.boot.world.field.objects.mob.Mob;
import ms.maplestory.boot.world.field.objects.npc.Npc;
import ms.maplestory.boot.loaders.MobData;
import ms.maplestory.boot.loaders.NpcData;
import ms.maplestory.boot.util.Position;
import ms.maplestory.boot.world.field.Field;
import lombok.Getter;
import lombok.Setter;
import ms.maplestory.boot.world.field.MobGen;

@Getter
@Setter
public class FieldObj {

    private int objectId = -1;
    protected int id;
    private int cy;
    private int fh;
    private int mobTime;
    private int rx0;
    private int rx1;
    private int x;
    private int y;
    private boolean hide;
    private boolean f;
    private String type = "";
    private Position position;
    private Field field;

    public FieldObj(int id) {
        this.id = id;
        this.position = new Position(0, 0);
    }

    public FieldObj deepCopy() {
        FieldObj copy = new FieldObj(getId());
        copy.setObjectId(getObjectId());
        copy.setType(getType());
        copy.setX(getX());
        copy.setY(getY());
        copy.setMobTime(getMobTime());
        copy.setHide(isHide());
        copy.setFh(getFh());
        copy.setCy(getCy());
        copy.setRx0(getRx0());
        copy.setRx1(getRx1());
        return copy;
    }

    public MobGen createMobGenFromLife() {
        MobGen mobGen = null;
        if (getType().equalsIgnoreCase("m")) {
            mobGen = new MobGen(0);
            mobGen.setPosition(getPosition().deepCopy());
            Mob mob = MobData.getMobDeepCopyById(getId());
            mob.setObjectId(getObjectId());
            mob.setType(getType());
            mob.setId(getId());
            mob.setX(getX());
            mob.setY(getY());
            mob.setPosition(new Position(getX(), getY()));
            mob.setMobTime(getMobTime());
            mob.setHide(isHide());
            mob.setFh(getFh());
            mob.setCy(getCy());
            mob.setRx0(getRx0());
            mob.setRx1(getRx1());
            mobGen.setMob(mob);
        }
        return mobGen;
    }

    public Npc createNpcFromDat() {
        Npc npc = null;
        if ("n".equalsIgnoreCase(getType())) {
            npc = NpcData.getNpcDeepCopyById(getId());
            npc.setObjectId(getObjectId());
            npc.setType(getType());
            npc.setX(getX());
            npc.setY(getY());
            npc.setPosition(new Position(getX(), getY()));
            npc.setMobTime(getMobTime());
            npc.setHide(isHide());
            npc.setFh(getFh());
            npc.setCy(getCy());
            npc.setRx0(getRx0());
            npc.setRx1(getRx1());
        }
        return npc;
    }

    public Reactor createReactorFromLife() {
        Reactor reactor = null;
        if ("r".equalsIgnoreCase(getType())) {
            reactor = new Reactor(getId());
            reactor.setPosition(getPosition().deepCopy());
        }
        return reactor;
    }

    @Override
    public String toString() {
        return String.format("ID: %d, ObjectID: %d, Position:" + position, id, objectId);
    }
}
